package com.luo.chengrui.wordprint;

import cn.hutool.core.bean.BeanUtil;

import java.util.*;
import java.util.stream.Collectors;

public class RenderDataFactory {

    private Object printData;

    private Map<String, Object> extendMap;
    private Map<String, MyPicture> imageMap;
    private Map<String, List<Map<String, String>>> tableRenderData;

    public RenderDataFactory(Object printData) {
        this.printData = printData;
        extendMap = new HashMap<>();
        imageMap = new HashMap<>();
        tableRenderData = new HashMap<>();
    }

    public RenderDataFactory() {
        extendMap = new HashMap<>();
        imageMap = new HashMap<>();
        tableRenderData = new HashMap<>();
    }

    public RenderDataFactory data(Object data) {
        this.printData = data;
        return this;
    }

    public RenderDataFactory addText(String key, Object val) {
        extendMap.put(key, val);
        return this;
    }

    /**
     * @param key       模板文件中图片占位的key，示例：{{%image}}
     * @param imagePath 图片路径，可为网络图片
     * @param width     图片宽度
     * @param height    图片高度
     * @return
     */
    public RenderDataFactory addImg(String key, String imagePath, Integer width, Integer height) {
        imageMap.put(key, new MyPicture(imagePath, width, height));
        return this;
    }

    public RenderDataFactory addTable(String key, List<Object> value) {
        List list = value;
        List<Map<String, String>> list1 = (List<Map<String, String>>) list.stream().map(row -> BeanUtil.beanToMap(row)).collect(Collectors.toList());
        tableRenderData.put(key, list1);
        return this;
    }


    /**
     * 可对审批意见进行修改等操作
     *
     * @return
     */
    public RenderData getData(RenderDataFunction function) {
        RenderData renderData = new RenderData();
        if (this.printData == null) {
            throw new RuntimeException("打印数据不能为空");
        }
        if (this.printData != null) {
            Map<String, Object> dataMap = BeanUtil.beanToMap(this.printData);
            dataMap.forEach((key, val) -> {
                val = function.edit(key, val);
                if (val instanceof List) {
                    List detail = (List) val;
                    List<Map<String, String>> list = (List<Map<String, String>>) detail.stream().map(row -> BeanUtil.beanToMap(row)).collect(Collectors.toList());
                    renderData.addTable(key, list);
                } else {
                    renderData.addText(key, String.valueOf(val));
                }
            });
        }
        extendMap.forEach((key, val) -> {
            val = function.edit(key, val);
            if (val instanceof List) {
                List detail = (List) val;
                List<Map<String, String>> list = (List<Map<String, String>>) detail.stream().map(row -> BeanUtil.beanToMap(row)).collect(Collectors.toList());
                renderData.addTable(key, list);
            } else {
                renderData.addText(key, String.valueOf(val));
            }
        });

        imageMap.forEach((key, val) -> {
            val = (MyPicture) function.edit(key, val);
            renderData.addPicture(key, val);
        });

        tableRenderData.forEach((key, val) -> {
            val = (List<Map<String, String>>) function.edit(key, val);
            renderData.addTable(key, val);
        });
        return renderData;
    }

    /**
     * 可对审批意见进行修改等操作
     *
     * @return
     */
    public RenderData getData() {
        RenderData renderData = new RenderData();
        if (this.printData == null) {
            throw new RuntimeException("打印数据不能为空");
        }
        Map<String, Object> dataMap = BeanUtil.beanToMap(this.printData);
        dataMap.forEach((key, val) -> {
            if (val instanceof List) {
                List detail = (List) val;
                List<Map<String, String>> list = (List<Map<String, String>>) detail.stream().map(row -> BeanUtil.beanToMap(row)).collect(Collectors.toList());
                renderData.addTable(key, list);
            } else {
                renderData.addText(key, String.valueOf(val));
            }
        });
        extendMap.forEach((key, val) -> {
            if (val instanceof List) {
                List detail = (List) val;
                List<Map<String, String>> list = (List<Map<String, String>>) detail.stream().map(row -> BeanUtil.beanToMap(row)).collect(Collectors.toList());
                renderData.addTable(key, list);
            } else {
                renderData.addText(key, String.valueOf(val));
            }
        });

        imageMap.forEach((key, val) -> {
            renderData.addPicture(key, val);
        });

        tableRenderData.forEach((key, val) -> {
            renderData.addTable(key, val);
        });
        return renderData;
    }

}
